package com.example.treasureadmin.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.treasureadmin.entity.MsgEntity;
import com.example.treasureadmin.service.GoodsService;
import com.example.treasureadmin.model.GoodsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/treasure-admin")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    private final static String[] allowFileType = new String[]{".bmp", ".jpg", ".jpeg", ".png", ".gif"};

    @GetMapping("/goods/all")
    public MsgEntity getAll()
    {
        List<GoodsModel> all = goodsService.getAll();
        return new MsgEntity("200","SUCCESS",all);
    }

    @GetMapping("/goods/{name}")
    public MsgEntity getUserId(@PathVariable("name") String name)
    {
        GoodsModel all = goodsService.findGoods(name);
        if(all == null)
        {
            return new MsgEntity("200","SUCCESS","商品不存在");
        }
        System.out.println(name);
        return new MsgEntity("200","SUCCESS",all);
    }

    @PutMapping("goods/change")
    public MsgEntity changeNumber(@RequestBody JSONObject requestBody)
    {
        int id = requestBody.getInteger("id");
        String imageUrl = requestBody.getString("image_url");
        String name = requestBody.getString("name");
        int number = requestBody.getInteger("number");
        int price = requestBody.getInteger("price");
        goodsService.changeGoods(id,imageUrl,name,number,price);
        return new MsgEntity("200","SUCCESS","修改成功");
    }

    @PostMapping("goods/add")
    public MsgEntity addGoods(@RequestParam("file") MultipartFile file,@RequestParam("name") String name,
                              @RequestParam("number") Integer number,@RequestParam("price") Integer price) throws IOException {

        if (file.isEmpty()){
            return new MsgEntity("200","ERROR","文件为空");
        }
        else{
            if (!Objects.requireNonNull(file.getOriginalFilename()).contains(".")){
                return new MsgEntity("200","ERROR","文件解析异常");
            }
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            if (suffix.isEmpty()|| !Arrays.asList(allowFileType).contains(suffix)){
                return new MsgEntity("200","ERROR","文件格式错误");
            }
            //上传到oss
            String result = goodsService.uploadImage(file.getInputStream(), suffix);
            goodsService.addGoods(result,name,number,price);
            return new MsgEntity("200","SUCCESS","添加成功");
        }
    }

    @DeleteMapping("/goods/{id}")
    public MsgEntity deleteGoods(@PathVariable("id") int id)
    {
        String code = goodsService.deleteGoods(id);
        if(code.equals("200"))
            return new MsgEntity("200","SUCCESS","修改成功");
        return new MsgEntity("200","SUCCESS","商品不存在");
    }
}
